from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlmodel import SQLModel, Session

"""
Author：LIL_HU
sqlite本地数据库连接
----------------------------------------------
username = ''
password = ''
host = ''  # 例如：'localhost' 或 '127.0.0.1'
port = ''  # 通常是 3306
database = ''
----------------------------------------------
数据库engine配置： 
echo=True,  		# 是否打印SQL
pool_size=10,  		# 连接池的大小，指定同时在连接池中保持的数据库连接数，默认:5
max_overflow=20,  	# 超出连接池大小的连接数，超过这个数量的连接将被丢弃,默认: 5
"""


class Database:
    def __init__(self):
        # 地址暂时写死
        sqlite_file_name = "database.db"
        sqlite_url = f"sqlite:///{sqlite_file_name}"
        connect_args = {"check_same_thread": False}
        self.engine = create_engine(sqlite_url, connect_args=connect_args)
        self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine)


sqlite_db = Database()
Base = declarative_base()


def create_db_and_tables():
    SQLModel.metadata.create_all(sqlite_db.engine)


def get_session():
    with Session(sqlite_db.engine) as session:
        yield session
